<?php
session_start();
require_once __DIR__.'/../../utils/checkTokenUtils.php';
require_once __DIR__ . '/../../model/UserModel.php';

use model\UserModel;
use utils\MysqlDBUtils;

$dbUtil = new MysqlDBUtils();

// 读取Session中的提示信息
$successMessage = $_SESSION['success_message'] ?? null;
$errorMessage = $_SESSION['error_message'] ?? null;

// 初始化模型并获取数据
$userModel = new UserModel();
$q = isset($_GET['q']) ? trim($_GET['q']) : '';
$currentPage = max(1, intval($_GET['page'] ?? 1));
$perPage = max(1, min(intval($_GET['per_page'] ?? 8), 100));
$paginationData = $userModel->getPaginatedUsers($q, $currentPage, $perPage);

// 提取分页数据为变量
extract($paginationData);

$title = '用户列表';
$description = '管理系统中的所有用户信息';
ob_start();
?>

<!-- 成功/错误提示 -->
<?php if ($successMessage): ?>
    <div id="successNotice"
         class="fixed top-20 left-1/2 transform -translate-x-1/2 bg-green-100 border border-green-400 text-green-700 px-4 py-2 rounded-lg shadow-lg max-w-md">
        <?= htmlspecialchars($successMessage) ?>
    </div>
<?php endif; ?>

<?php if ($errorMessage): ?>
    <div id="errorNotice"
         class="fixed top-20 left-1/2 transform -translate-x-1/2 bg-red-100 border border-red-400 text-red-700 px-4 py-2 rounded-lg shadow-lg max-w-md">
        <?= htmlspecialchars($errorMessage) ?>
    </div>
<?php endif; ?>

<!-- 清除已显示的消息 -->
<?php
unset($_SESSION['success_message'], $_SESSION['error_message']);
?>

<!-- 页面专属内容 -->
<div class="mb-4 flex gap-2 items-center justify-between">
    <button id="addUserBtn"
            class="add-btn px-3 py-1 bg-blue-500 text-white rounded-md hover:bg-blue-600 transition-colors ">新增用户
    </button>
    <!-- 模糊搜索表单 -->
    <?php //include __DIR__.'/partials/search_form.php'; ?>
</div>

<table class="w-full">
    <thead>
    <tr class="bg-gray-100">
        <th class="p-3">ID</th>
        <th class="p-3">用户名</th>
        <th class="p-3">密码</th>
        <th class="p-3">真实姓名</th>
        <th class="p-3">电话</th>
        <th class="p-3">邮箱</th>
        <th class="p-3">权限</th>
        <th class="p-3">注册时间</th>
        <th class="p-3">操作</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($users as $user): ?>
        <tr class="border-b">
            <td class="p-3 text-center"><?php echo htmlspecialchars($user['id']) ?></td>
            <td class="p-3 text-center"><?php echo htmlspecialchars($user['username']); ?></td>
            <td class="p-3 text-center">*******</td>
            <td class="p-3 text-center"><?php echo htmlspecialchars($user['name']); ?></td>
            <td class="p-3 text-center"><?php echo htmlspecialchars($user['phone']); ?></td>
            <td class="p-3 text-center"><?php echo htmlspecialchars($user['email'])?></td>
            <td class="p-3 text-center"><?php echo ($user['role'] ?? 0) == 1 ? '管理员' : '读者' ?></td>
            <td class="p-3 text-center"><?php echo htmlspecialchars($user['create_time']); ?></td>
            <td class="p-3 text-center">
                <button
                        class="edit-btn px-3 py-1 bg-blue-500 text-white rounded-md hover:bg-blue-600 transition-colors mr-2"
                        data-id="<?= htmlspecialchars($user['id']) ?>"
                        data-username="<?= htmlspecialchars($user['username']) ?>"
                        data-name="<?= htmlspecialchars($user['name'])?>"
                        data-phone="<?= htmlspecialchars($user['phone'])?>"
                        data-email="<?= htmlspecialchars($user['email'])?>"
                        data-role="<?= htmlspecialchars($user['role']?? 0)?>"
                >
                    编辑
                </button>
                <button class="delete-btn px-3 py-1 bg-red-500 text-white rounded-md hover:bg-red-600 transition-colors"
                        data-delete-url="DeleteUser.php?id=<?= htmlspecialchars($user['id']) ?>">删除
                </button>
            </td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>


<!-- 分页控件 -->
<?php include __DIR__ . '/../partials/pagination.php'; ?>
<?php
$content = ob_get_clean();
include __DIR__ . '/../layouts/Main.php'; // 引入主布局
// 引入删除确认模态框
include __DIR__ . '/../partials/del_modal.php';
?>

<!-- 引入编辑模态框模板 -->
<?php include __DIR__ . '/partials/edit_modal.php'; ?>
<!-- 引入添加用户模态框模板 -->
<?php include __DIR__ . '/partials/add_modal.php'; ?>

<script>
    function deleteUser(userId) {
        if (confirm('确定要删除这个用户吗？')) {
            const form = document.createElement('form');
            form.method = 'post';
            form.action = 'DeleteUser.php';

            const idInput = document.createElement('input');
            idInput.type = 'hidden';
            idInput.name = 'id';
            idInput.value = userId;
            form.appendChild(idInput);

            document.body.appendChild(form);
            form.submit();
        }
    }
</script>